package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import com.zhijian.medical.dao.local.DeviceTrainHistoryMapper;
import com.zhijian.medical.entity.po.DeviceTrainHistory;
import com.zhijian.medical.entity.vo.request.DeviceTrainHistorySearchVo;
import com.zhijian.medical.entity.vo.response.DeviceTrainHistoryVo;
import com.zhijian.medical.service.DeviceTrainHistoryService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 设备-培训记录业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-07-13 16:35:19
 */
@Slf4j
@Service
public class DeviceTrainHistoryServiceImpl implements DeviceTrainHistoryService {

    @Autowired
    private DeviceTrainHistoryMapper mapper;

    @Override
    public DeviceTrainHistory selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DeviceTrainHistoryVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(DeviceTrainHistory deviceTrainHistory) {
        Date now = new Date();
        deviceTrainHistory.setCreateTime(now);
        deviceTrainHistory.setUpdateTime(now);
        return mapper.insert(deviceTrainHistory) > 0 ? deviceTrainHistory.getId() : 0L;
    }

    @Override
    public boolean update(DeviceTrainHistory deviceTrainHistory) {
        Date now = new Date();
        deviceTrainHistory.setUpdateTime(now);
        return mapper.updateByPrimaryKey(deviceTrainHistory) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(DeviceTrainHistory record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(DeviceTrainHistorySearchVo deviceTrainHistorySearchVo) {
        return mapper.count(deviceTrainHistorySearchVo);
    }

    @Override
    public List<DeviceTrainHistoryVo> selectList(DeviceTrainHistorySearchVo deviceTrainHistorySearchVo) {
        return mapper.selectList(deviceTrainHistorySearchVo);
    }

    @Override
    public Page<DeviceTrainHistoryVo> selectPage(DeviceTrainHistorySearchVo deviceTrainHistorySearchVo) {
        //先直接查总数
        int count = count(deviceTrainHistorySearchVo);
        //分页
        Page<DeviceTrainHistoryVo> page = new Page<>(true, count, deviceTrainHistorySearchVo.getPageNum(), deviceTrainHistorySearchVo.getPageSize());
        deviceTrainHistorySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DeviceTrainHistoryVo> responseVos = this.selectList(deviceTrainHistorySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<DeviceTrainHistory> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    public static final String CODE_PREFIX = "BLK-SOP-033-02";

    @Override
    public String selectMaxCode() {
        return mapper.selectMaxCodeByDay(DateUtil.formatDate(new Date()));
    }

    @Override
    public String createNextCode() {
        String maxCode = this.selectMaxCode();
        if (StringUtils.isBlank(maxCode)) {
            return String.format("%s-%s-1", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")));
        }
        String[] maxCodeArray = maxCode.split("-");
        Integer i = Integer.valueOf(maxCodeArray[maxCodeArray.length - 1]);
        i++;
        return String.format("%s-%s-%s", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")), i);
    }

}